package org.zzz.hepotaiya.executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zzz.hepotaiya.beans.BatchResult;
import org.zzz.hepotaiya.config.BatchConfig;
import org.zzz.hepotaiya.config.ConfigParser;
import org.zzz.hepotaiya.container.ContainerImpl;
import org.zzz.hepotaiya.container.IContainer;
import org.zzz.hepotaiya.exception.ParseException;
import org.zzz.hepotaiya.loader.BatchLoader;
import org.zzz.hepotaiya.loader.BatchLoaderImpl;

/**
 * 
 *
 */
public abstract class AbstractExecutor implements BatchExecutor {
	
	List<BatchResult> resultList = new ArrayList<BatchResult>();

	Map<String, Boolean> resultMap = new HashMap<String, Boolean>();

	IContainer container = new ContainerImpl();

	boolean isError = false;
	
	BatchLoader batchLoader = new BatchLoaderImpl();
	
	ConfigParser configParser;

	/*
	 * (non-Javadoc)
	 * @see org.zzz.hepotaiya.executor.BatchExecutor#setContainer(org.zzz.hepotaiya.container.IContainer)
	 */
	public void setContainer(IContainer container) {
		this.container = container;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.zzz.hepotaiya.executor.BatchExecutor#setBatchLoader(org.zzz.hepotaiya.loader.BatchLoader)
	 */
	public void setBatchLoader(BatchLoader batchLoader) {
		this.batchLoader = batchLoader;
	}

	/*
	 * (non-Javadoc)
	 * @see org.zzz.hepotaiya.executor.BatchExecutor#setConfigParser(org.zzz.hepotaiya.config.ConfigParser)
	 */
	public void setConfigParser(ConfigParser configParser) {
		this.configParser = configParser;
	}
	
	/**
	 * 
	 */
	void setUp(){
		this.batchLoader.setContainer(this.container);
	}
	
	/**
	 * 依存関係にマッチしているか確認します。
	 */
	String checkDepency(Map<String, Boolean> depency) {
		for (Map.Entry<String, Boolean> entry : depency.entrySet()) {
			String key = entry.getKey();
			Boolean value = entry.getValue();
			if (this.resultMap.containsKey(key)) {
				// 実行済み
				if (value != this.resultMap.get(key)) {
					// 依存チェック失敗
					return  key;
				}
			} else if (value) {
				// 未実行, 成功依存
				return key;
			}
		}
		return null;
	}
	
	List<BatchConfig> getConfigList() throws ParseException {
		if (null == this.configParser) {
			return null;
		}
		return this.configParser.parse();
	}
}
